﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BranchOfSansar.Sansar.Transfer;

namespace BranchOfSansar.Register.Trans
{
    public partial class frmTransSell : BranchOfSansar.Register.Trans.frmTransBase
    {

        private List<Transaction> txn = new List<Transaction>();
        public frmTransSell()
        {
            InitializeComponent();
        }
        public frmTransSell(long contract_no):this()
        {
            SetContractNo(contract_no);
            frmTransSell_ContractSelected();
        }

        private void frmTransSell_ContractSelected()
        {
            pnlTransfer.Enabled = true;
            btnSave.Enabled = true;
            using (BranchOfSansar.Sansar.Enquiry.EnquiryClient tc = new BranchOfSansar.Sansar.Enquiry.EnquiryClient(Globals.Token))
            {
                txtProdCode.Properties.DisplayMember = "EquipCode";
                txtProdCode.Properties.ValueMember = "EquipCode";
                txtProdCode.Properties.DataSource = tc.Equipments();
            }
        }

        private void frmTransSell_Load(object sender, EventArgs e)
        {
            txtDate.DateTime = DateTime.Now;
        }

        private void txtProdCode_EditValueChanged(object sender, EventArgs e)
        {
            BranchOfSansar.Sansar.Enquiry.Equipment p = (BranchOfSansar.Sansar.Enquiry.Equipment)txtProdCode.Properties.GetDataSourceRowByDisplayValue(txtProdCode.Text);
            txtProdDescription.Text = string.Format("{0}, {1}, {2}", p.Name, p.Price, p.Unit);
            txtPrice.Value = p.Price;
        }

        private void TxtEditValueChanged(object sender, EventArgs e)
        {
            txtTotalPrice.Value = txtPrice.Value * txtQuantity.Value;
        }

        private void Bind()
        {
            gridTransfer.DataSource = txn;
            gridTransfer.RefreshDataSource();
            decimal sum=0;
            foreach(var i in txn)
                sum+=i.TxnAmount;
            txtAmount.Value = sum;
        }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            BranchOfSansar.Sansar.Enquiry.Equipment p = (BranchOfSansar.Sansar.Enquiry.Equipment)txtProdCode.Properties.GetDataSourceRowByDisplayValue(txtProdCode.Text);
            if (p == null)
            {
                Globals.Warr("Төхөөрөмж сонгоогүй байна.");
                return;
            }

            txn.Add(new Transaction()
            {
                ContractNo = Contract.ContractNo,
                EquipCode = p.EquipCode,
                Quantity = (int)txtQuantity.Value,
                StartDate = null,
                EndDate = null,
                Status = 1,
                TxnCategory = ((BranchOfSansar.Sansar.Enquiry.ComboItem)cmbTransCategory.SelectedItem).StrValue,
                BankNo = cmbBank.Enabled ? ((BranchOfSansar.Sansar.Enquiry.ComboItem)cmbBank.SelectedItem).IntValue : 0,
                TxnPrice = txtPrice.Value,
                TxnAmount = txtTotalPrice.Value,
                TxnDebtAmount = txtTotalPrice.Value,
                TxnDate = txtDate.DateTime,
                TxnDesc = p.Name,
                TxnType = 2100,
                UserNo = Globals.UserId
            }
            );

            Bind();
        }

        private void gridTransfer_DoubleClick(object sender, EventArgs e)
        {
            if(viewTransfer.SelectedRowsCount>0)
            {
                Transaction t=(Transaction)viewTransfer.GetRow(viewTransfer.GetSelectedRows()[0]);
                txn.Remove(t);
                Bind();                
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (txn.Count > 0)
            {
             
                SendContract con = new SendContract()
                {
                    ContractNo = Contract.ContractNo,
                    Balance = decimal.Parse(txtBalance.Text),
                    CurrProdCode = int.Parse(txtProdCode.Text),
                    EndDate = Contract.EndDate ?? txtDate.DateTime,
                    StartDate = Contract.StartDate ?? txtDate.DateTime
                };


                using (TransferClient tc = new TransferClient(Globals.Token))
                {
                    var res = tc.DoTransaction(txn.ToArray(),new SendTransfer[]{}, con);
                    if (res.Code != 0)
                        Globals.Warr(res.Message);
                    else
                    {
                        Globals.Info(res.Message);
                        this.DialogResult = System.Windows.Forms.DialogResult.OK;
                    }
                }
            }
        }

        private void btnCancel_Click_1(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }
    }
}
